<template>
  <div id="nav">
    <button @click="$store.commit('toggle')">
      nickname：{{ $store.state.user.username }}
    </button>
    <!-- composition 写法 -->
    <button @click="toggle">nickname：{{ username }}</button>
    <router-link to="/">Home</router-link> |
    <router-link to="/about">About</router-link> |
    <router-link to="/todo-list">TodoList</router-link> |
    <router-link to="/indexed-db">IndextedDB</router-link>
  </div>

  <nav-link to="/">dashboard</nav-link> |
  <nav-link to="/about">about</nav-link> |
  <nav-link to="/todo-list">TodoList</nav-link> |
  <nav-link to="/indexed-db">IndextedDB</nav-link>

  <!-- <router-view /> -->
  <router-view v-slot="{ Component }">
    <keep-alive>
      <component :is="Component" />
    </keep-alive>
  </router-view>
</template>
<script>
import NavLink from "./components/NavLink.vue";
import { useStore } from "vuex";
import { toRefs } from "@vue/reactivity";
/*

// data统一改成 函数形式而不是对象形式
createApp({
  data() {
    return {
      apiKey: 'a1b2c3'
    }
  }
}).mounted('#app')
*/
export default {
  components: { NavLink },
  setup() {
    const store = useStore(); // 在store中真正响应式的只有带有Proxy的属性
    console.log(store);
    return {
      ...toRefs(store.state.user),
      toggle() {
        store.commit("toggle");
      },
    };
  },
  data() {
    return {};
  },
};
</script>

<style lang="scss">
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
}

#nav {
  padding: 30px;

  a {
    font-weight: bold;
    color: #2c3e50;

    &.router-link-exact-active {
      color: #42b983;
    }
  }
}
.active {
  background-color: #3fb983;
}
</style>
